const express = require("express");
const util = require("../common/util");
const WarehouseDB = require("../model/db/warehouse");

/**  
 * 新增仓库  
 * @param {express.Request} req 请求  
 * @param {express.Response} res 响应  
 */
async function add(req, res) {
    try {
      const warehouse = {
        name: req.body.name,
        location: req.body.location,
        size: req.body.size,
        totalCapacity: req.body.totalCapacity,
        maxHeight: req.body.maxHeight,
        description: req.body.description,
        manager: req.body.manager,
        active: req.body.active,
        equipment: req.body.equipment,
        securityLevel: req.body.securityLevel,
        storageType: req.body.storageType
      };

      const db = WarehouseDB.getInstance();

      const lastID = await db.add(warehouse);

      util.log(`新增仓库信息：lastID=${lastID}`);

      res.json(util.FormatJSONData(201, `新增仓库信息成功`, { lastID }));
    } catch (error) {
      // 处理错误  
      util.err(error);
      res.status(500).json(util.FormatJSONData(500, '服务器内部错误', error));
    }
}

/**
 * 更新仓库
 * @param {express.Request} req 请求
 * @param {express.Response} res 响应
 */
async function update(req, res) {
    try {
      const warehouseId = req.params.id;
      const warehouse = {
        name: req.body.name,
        location: req.body.location,
        size: req.body.size,
        totalCapacity: req.body.totalCapacity,
        maxHeight: req.body.maxHeight,
        description: req.body.description,
        manager: req.body.manager,
        active: req.body.active,
        equipment: req.body.equipment,
        securityLevel: req.body.securityLevel,
        storageType: req.body.storageType,
        id: warehouseId  // 添加仓库ID字段，以便于更新操作
      };

      const db = WarehouseDB.getInstance();

      const result = await db.update(warehouse); // 使用update方法

      // 检查受影响的行数
      if (result.affectedRows === 0) {
        throw new Error('没有找到匹配的仓库记录');
      }

      util.log(`更新仓库信息：仓库ID=${warehouseId }`);

      res.json(util.FormatJSONData(200, `更新仓库信息成功`, { warehouseId  }));
    } catch (error) {
      // 处理错误
      util.err(error);
      res.status(500).json(util.FormatJSONData(500, '服务器内部错误', error));
    }
}

/**  
* 查询指定仓库详情
* @param {express.Request} req 请求  
* @param {express.Response} res 响应  
*/
async function find(req, res) {
    try {
      const warehouseId = req.params.id;
      const db = WarehouseDB.getInstance();

      const warehouse = await db.find(warehouseId);
     
      if (!warehouse) {
        // 如果找不到仓库，返回 404 响应
        res.status(404).send('没有找到该仓库');
      } else {

        res.json(util.FormatJSONData(201, `查找仓库信息成功`, { warehouse }));
      }

    } catch (error) {
      // 处理错误  
      util.err(error);
      res.status(500).json(util.FormatJSONData(500, '服务器内部错误', error));
    }
}

/**  
* 查询所有仓库详情
* @param {express.Request} req 请求  
* @param {express.Response} res 响应  
*/
async function findAll(req, res) {
    try {
      const db = WarehouseDB.getInstance();

      const warehouse = await db.findAll();
     
      if (!warehouse) {
        // 如果找不到仓库，返回 404 响应
        res.status(404).send('获取仓库列表失败');
      } else {

        res.json(util.FormatJSONData(201, `获取仓库列表成功`, { warehouse }));
      }

    } catch (error) {
      // 处理错误  
      util.err(error);
      res.status(500).json(util.FormatJSONData(500, '服务器内部错误', error));
    }
}

/**  
* 删除指定仓库
* @param {express.Request} req 请求  
* @param {express.Response} res 响应  
*/
async function remove(req, res) {
    try {
      const warehouseId = req.params.id;
      const db = WarehouseDB.getInstance();

      const warehouse = await db.remove(warehouseId);
     
      if (!warehouse) {
        // 如果找不到仓库，返回 404 响应
        res.status(404).send('未查找到该仓库');
      } else {
        res.json(util.FormatJSONData(201, `删除仓库信息成功`, { warehouse }));
      }

    } catch (error) {
      // 处理错误  
      util.err(error);
      res.status(500).json(util.FormatJSONData(500, '服务器内部错误', error));
    }
}


module.exports = {
  add,
  find,
  findAll,
  remove,
  update
};